﻿namespace MockingBird.Simulator.Management
{
    using System.IO;
    using MockingBird.Simulator.Framework.Configuration;

    public class ConfigWriter : IConfigWriter
    {
        private HandlerConfiguration serviceConfig;
        private ServiceConfigurationSettings serviceConfigSettings;
        private IFolderManager folderManager;
        private IConfigurationFileStream configFileStream;

        public ConfigWriter() : this(new FolderManager(), new ConfigurationFileStream()) { }

        public ConfigWriter(IFolderManager targetFolderManager, IConfigurationFileStream targetConfigFileStream)
        {
            this.folderManager = targetFolderManager;
            this.configFileStream = targetConfigFileStream;

        }

        public void WriteConfiguration(HandlerConfiguration configData, ServiceConfigurationSettings configSettings)
        {
            this.serviceConfig = configData;
            this.serviceConfigSettings = configSettings;
            this.folderManager.CreateServiceFolders(this.serviceConfigSettings);
            string targetFileName = Path.Combine(this.folderManager.GetConfigFolderRoot(), this.serviceConfigSettings.ConfigFileName);
            this.configFileStream.PersistConfiguration(targetFileName, this.serviceConfig);
        }
    }
}
